package com.share.dubbo.wssh.config;

import ch.qos.logback.classic.spi.Configurator;
import com.baidu.disconf.client.common.annotations.DisconfFile;
import com.baidu.disconf.client.common.annotations.DisconfUpdateService;
import com.baidu.disconf.client.common.update.IDisconfUpdate;
import lombok.extern.slf4j.Slf4j;
import org.apache.tomcat.util.file.ConfigurationSource;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.net.URL;

/**
 * 自动加载log4j2.xml配置（由于新部署项目还没有从disconf配置中心下载到log4j2.xml配置）
 *
 * 在disconf统一配置中心后台改动会自动重新加载最新配置
 *
 * @author YANGLiiN 2017-09-05 13:41
 */

//@Slf4j
//@Component
//@DisconfFile(filename = com.share.dubbo.wssh.config.Log4j2Disconf.LOG4J2_CONFIG_NAME)
//@DisconfUpdateService(classes = {com.share.dubbo.wssh.config.Log4j2Disconf.class})
public class Log4j2Disconf implements InitializingBean,
        IDisconfUpdate {

  public static final String LOG4J2_CONFIG_NAME = "log4j2.xml";

  @Override
  public void afterPropertiesSet() throws Exception {
    loadProperties();
  }

  @Override
  public void reload() throws Exception {
    loadProperties();
  }

  private void loadProperties() throws FileNotFoundException {
    URL url = Log4j2Disconf.class.getResource("/" + LOG4J2_CONFIG_NAME);

    ConfigurationSource  source = null ; // new ConfigurationSource(new FileInputStream(new File(url.getPath())),url);
  }
}
